#! /usr/bin/env python
# -*- coding: utf-8 -*-
# #############################################################
# Ce script permet de recuperer l'heure et la date a partir d'un serveur nntp
# This program helps to get date and time from an nntp server
#
# Ce programme est livree sans aucune garantie sous les terme de la licence GPL
# this program is distributed under the terms of the General Public Licence 
# without any gwaranty
# 
# Edite par mzalim 
# sous licence GPL 
# Un programmeur ne copie pas il s'inspire , lire les termes de la licence gpl.txt
# 
# Version du script 0.1.0
# Date de production Sun Dec 21 03:20:17 2008
#
# For more informations : http://code.google.com/p/moufid/
#
# #############################################################
#http://code.google.com/p/moufid/


from utils import *
import nntplib, sys


#montre l'aide
def usage():
	""" usage instruction"""
	print "usage :", sys.argv[0], "server_address [port] [login] [password]"
	print ""
	print "your login and password wil not be saved or stored \n encryption not suported" 	
	END()

#fonction pricipale
def main():
	
	_len = len(sys.argv) - 1
	
	if _len == 0 :
		usage()
		END(-1)
	elif _len == 1 :
		_nntp = nntplib.NNTP(sys.argv[1])
	elif _len == 2 :
		_nntp = nntplib.NNTP(sys.argv[1],INT(sys.argv[2]))
	elif _len == 3 :
		_nntp = nntplib.NNTP(sys.argv[1],INT(sys.argv[2]), sys.argv[3])
	elif _len == 4 :
		_nntp = nntplib.NNTP(sys.argv[1],INT(sys.argv[2]), sys.argv[3], sys.argv[4])
	else :
		usag()
		END()	
	
	#print _nntp.getwelcome()
	
	#recuperer la date
	(_nntp_response ,_nntp_date, _nntp_time) = _nntp.date()

	if _nntp_response[0:3]  == "111" :
		print "Date  : %s/%s/200%s" %(_nntp_date[4:6],_nntp_date[2:4], _nntp_date[0:2] )
		print "Heure : %s:%s:%s" %(_nntp_time[0:2],_nntp_time[2:4], _nntp_time[4:6] )
	
if __name__ == "__main__" :
	if len (sys.argv) > 1 :
		main()
	else :
		usage()
